<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @submit="handleSearch" >
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="类型">
                <!-- <a-input v-model="queryParam.name" placeholder="" /> -->
                <a-select v-model="queryParam.type" placeholder="请选择">
                  <a-select-option v-for="it in blackTypeList" :key="it.id" :value="it.id">{{ it.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="时间">
                <a-range-picker
                  show-time
                  name="buildTime"
                  style="width: 100%"
                  v-model="queryParam.time"
                />
              </a-form-item>
            </a-col>

            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch" htmlType="submit">查询</a-button>
                <a-button
                  style="margin-left: 8px"
                  @click="() => ((this.queryParam = {}), this.$refs.table.refresh(true))"
                  >重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd" >新建</a-button>
      </div>

      <s-table ref="table" size="default" rowKey="id" :columns="columns" :data="loadData" >
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleDel(record)">删除</a>
          </template>
        </span>
        
        <span slot="content" slot-scope="text">
          <div class="remark-wrap">{{text}}</div>
        </span>
        <span slot="remark" slot-scope="text">
          <div class="remark-wrap">{{text}}</div>
        </span>
        
        <span slot="cType" slot-scope="text">
          <!-- <span>{{text}}</span> -->
          <!-- <a-tag color="green">green</a-tag> -->
          <a-tag :color="text | statusFilter(blackTypeList, 'status')">
            <!-- <template #icon>
              <a-icon type="dingding"/>
            </template> -->
            {{ text | statusFilter(blackTypeList, 'name') }}
          </a-tag>
        </span>
      </s-table>
      <blank-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        :typeArr="blackTypeList"
        @cancel="handleCancel"
        @ok="handleOk"
      />
      <!-- <step-by-step-modal ref="modal" @ok="handleOk" /> -->
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import {
  WifiOutlined
} from '@ant-design/icons-vue';
import cloneDeep from 'lodash.clonedeep'
import { STable, Ellipsis } from '@/components'
import { blackListAdd,
blackListList,
blackListUpdate } from '@/api/system'
import BlankForm from './modules/blankForm'
import { Modal } from 'ant-design-vue'
import { blackTypeList } from '@/utils/other'
const columns = [
  // id、类型、内容、备注、截至时间、添加人、添加时间、最后编辑人、最后编辑时间
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    fixed: true,
  },
{
    title: '内容',
    dataIndex: 'content',
    scopedSlots: { customRender: 'content' },
  },
  {
    title: '类型',
    dataIndex: 'cType',
    scopedSlots: { customRender: 'cType' },
  },
  
  {
    title: '备注',
    dataIndex: 'remark',
    scopedSlots: { customRender: 'remark' },
  },
  {
    title: '截至时间',
    dataIndex: 'upToTime',
    key: 'upToTime',
  },
  {
    title: '添加人',
    dataIndex: 'addByName',
    customRender: (text) => text || '--',
  },
  {
    title: '添加时间',
    dataIndex: 'addTime',
    key: 'addTime',
    customRender: (text) => text || '--',
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'AWM-blankList',
  components: {
    STable,
    Ellipsis,
    BlankForm,
    WifiOutlined
  },
  data() {
    this.columns = columns
    return {
      blackTypeList,
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParam: {},
      data: [],
      sortJ: {
        levelNum: 'up_to_time',
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        let sortJ = this.sortJ
        if (parameter.sortField) {
          parameter.sortJ = sortJ
        }
        let queryParam = cloneDeep(this.queryParam)
        if(queryParam.time){
          queryParam.startTime = queryParam.time[0].format('YYYY-MM-DD HH:mm:ss')
          queryParam.endTime = queryParam.time[1].format('YYYY-MM-DD HH:mm:ss')
        }
        delete queryParam.time
        const requestParameters = Object.assign({}, parameter, queryParam)
        return blackListList(requestParameters).then((res) => {
          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
    }
  },

  created() {
    // this.getList()
    // getRoleList({ t: new Date() })
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange,
      }
    },
  },
  methods: {
    handleSearch(e) {
      e.preventDefault()
      this.$refs.table.refresh(true)
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    saveCB() {
      const form = this.$refs.createModal.form
      this.visible = false
      this.confirmLoading = false
      // 重置表单数据
      form.resetFields()
      // 刷新表格
      this.$refs.table.refresh()
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        
        if (!errors) {
          if (values.id > 0) {
            blackListUpdate(values)
              .then((res) => {
                this.saveCB()
                this.$message.success('修改成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          } else {
            delete values.id
            let value = cloneDeep(values)
            console.log(value)
            value.upToTime = value.upToTime.format('YYYY-MM-DD HH:mm:ss')//.replace("T",'')
            blackListAdd(value)
              .then((res) => {
                this.saveCB()
                this.$message.success('新增成功!')
              })
              .catch((err) => {
                this.saveCB()
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub(record) {
      if (record.status !== 0) {
        this.$message.info(`${record.no} 订阅成功`)
      } else {
        this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleDel(a_record) {
      let self = this
      const modal = this.$confirm({
        title: '级别删除',
        content: `是否删除级别:${a_record.content}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal),
        onCancel: () => {},
      })
    },
    handleDelete(a_id, a_modal) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: -1 }
        blackListUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
  },
}
</script>

 